﻿using Microsoft.Extensions.Caching.Distributed;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using UpperComputer.Application;
using UpperComputer.WPF.Entry.Common;

namespace UpperComputer.WPF.Entry.ViewModels
{
    public class MenuSelectViewModel : BaseViewModel
    {
        private readonly BaseMenuService _menuService;

        public readonly string _code;

        public ObservableCollection<BaseMenuResultDto> Menus { get; set; } = new ObservableCollection<BaseMenuResultDto>();

        public MenuSelectViewModel(BaseMenuService menuService)
        {
            _menuService = menuService;
            _code = DistributedCache.Get<string>(Constant.CACHE_KEY_MENU_CODE);
        }

        protected override async Task LoadAsync(object parameter, CancellationToken cancellationToken)
        {
            List<BaseMenuResultDto> menus = await _menuService.GetMenus(new BaseMenuResultRequestDto { Code = _code, IsActive = true }, cancellationToken);
            foreach (BaseMenuResultDto item in menus)
            {
                Menus.Add(item);
            }
        }
    }
}